package id.co.shuba.spamobile.util;

import java.util.List;

import id.co.shuba.spamobile.data.DatabaseHandler;
import id.co.shuba.spamobile.data.StandardDB;
import id.co.shuba.spamobile.model.StandardInfo;
import id.co.shuba.spamobile.widget.StandardAdapter;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SpinnerUtil {
	public static void fillStandard(Activity ctx, StandardDB db, Spinner spinner, String group, String defaultOption) {
		List<StandardInfo> list = db.getList(group);
		list.add(0, new StandardInfo(defaultOption, defaultOption));
		StandardAdapter adapter = new StandardAdapter(ctx, list);
		spinner.setAdapter(adapter);
	}
	
	public static void fillStandard(Activity ctx, StandardDB db, Spinner spinner, String group, String type, String defaultOption) {
		List<StandardInfo> list = db.getList(group, type);
		list.add(0, new StandardInfo(defaultOption, defaultOption));
		StandardAdapter adapter = new StandardAdapter(ctx, list);
		spinner.setAdapter(adapter);
	}
	
	public static void areaSetSelectedValue(Spinner spinner, String value) {		
		@SuppressWarnings("unchecked")
		ArrayAdapter<String> adapter = (ArrayAdapter<String>)spinner.getAdapter();
		//adapter.
		spinner.setSelection(adapter.getPosition(value), false);				
	}
	
	public static String areaGetSelectedValue(Spinner spinner, String value) {
		String strValue = (String)spinner.getSelectedItem();
		return value.equals(strValue)? null : strValue;
	}
	
	public static String optionGetSelectedValue(Spinner spinner, String defaultOption) {
		StandardInfo info = (StandardInfo) spinner.getItemAtPosition(spinner
				.getSelectedItemPosition());
		if (info == null)
			return null;
		
		String value = info.getValue();
		return defaultOption.equals(value)? null : value;
	}
	
	public static StandardInfo optionGetSelectedItem(Spinner spinner) {
		StandardInfo info = (StandardInfo) spinner.getItemAtPosition(spinner
				.getSelectedItemPosition());
		return info;
	}

	public static void optionSetSelectedValue(Spinner spinner, String id) {
		StandardAdapter adapter = (StandardAdapter)spinner.getAdapter();
		spinner.setSelection(adapter.getPosition(id));		
	}
}
